﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;
namespace BHair.Base
{
    public partial class frmhycz : Form
    {
        public frmhycz()
        {
            InitializeComponent();
        }
        string ckr = "";//持卡人
        decimal ye;//余额
        decimal sjcz = 0;//实际充值
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        string siweils = "";//四位流水号
        private void frmhycz_Load(object sender, EventArgs e)
        {
            djh();
            jiazai();
            shifoudayin();
            skzh();

        }
        public void djhshuaxin()
        {
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();

                string sql = string.Format("UPDATE [BHairDB].[dbo].[djh] SET [hycz] = '{0}'", siweils);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
            }
            catch { }
        }
        //加载单据号
        public void jiazai()
        {
            /* string bs = "CZ-";
             string time = DateTime.Now.ToString("yyyy-MM-dd");
             Random ran = new Random();
             int RandKey = ran.Next(10, 999);
             string djh = bs + time + "-" + RandKey.ToString();
             this.txtdjh.Text = djh;
             */
            this.txtcardID.Text = null;
            this.txtckr.Text = null;
            this.txtczbz.Text = null;
            this.txtssje.Text = "0";
            this.txtzsje.Text = "0";
            this.txtye.Text = "0";
            this.txtsjcz.Text = null;
        }
        //显示收款账户
        public void skzh()
        {
            SqlConnection conn = new SqlConnection(constr);
            string sql = "SELECT[zhID],[zhname]FROM[BHairDB].[dbo].[yhzh]";
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "yhzh");
            comboBox1.DataSource = ds.Tables["yhzh"];
            comboBox1.DisplayMember = "zhname";
            conn.Close();

        }
        public void djh()
        {
            string lsh = "";
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                string sql = "SELECT [hycz] FROM [BHairDB].[dbo].[djh]";
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                while (sdr.Read())
                {
                    lsh = sdr["hycz"].ToString();
                }
                string bs = "CZ-";
                string time = DateTime.Now.ToString("yyyy-MM-dd");
                /*
                Random ran = new Random();
                int RandKey = ran.Next(10, 999);
                */
                int djlsh = Int32.Parse(lsh) + 1;
                siweils = string.Format("{0:00000}", djlsh);
                string djh = bs + time + "-" + chuanzhi.banzu + siweils;
                this.txtdjh.Text = djh;
                conn.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            if (txtcardID.Text != string.Empty)
            {
                chuanzhi ca = new chuanzhi();
                if (ca.CheckPass(txtcardID.Text) == true)
                {
                    if (ca.Checkyxq(txtcardID.Text) == true)
                    {

                        try
                        {
                            conn.Open();
                            string sql = string.Format("SELECT [mname],[mbalance] FROM [BHairDB].[dbo].[member] where mid='{0}'", txtcardID.Text);
                            SqlCommand comm = new SqlCommand(sql, conn);
                            SqlDataReader sdr = comm.ExecuteReader();
                            while (sdr.Read())
                            {
                                ckr = sdr["mname"].ToString();
                                ye = decimal.Parse(sdr["mbalance"].ToString());
                            }
                            txtckr.Text = ckr;
                            txtye.Text = ye.ToString();
                            conn.Close();
                        }
                        catch (Exception ex)
                        {
                            conn.Close();
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("此卡已过期！");
                        txtcardID.Text = null;
                    }
                }
                else
                {
                    MessageBox.Show("卡号不存在！");
                    txtcardID.Text = null;
                }
            }
            else
            {
                MessageBox.Show("请输入卡号");
            }
        }

        private void txtssje_TextChanged(object sender, EventArgs e)
        {
            sjcz = decimal.Parse(txtssje.Text) + decimal.Parse(txtzsje.Text);
            txtsjcz.Text = sjcz.ToString();
        }

        private void txtzsje_TextChanged(object sender, EventArgs e)
        {
            sjcz = decimal.Parse(txtssje.Text) + decimal.Parse(txtzsje.Text);
            txtsjcz.Text = sjcz.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlConnection conn1 = new SqlConnection(constr);
            try
            {
                string datatime = DateTime.Now.ToString();
                //插入到会员充值表
                string sql1 = string.Format("INSERT INTO [BHairDB].[dbo].[deposit]"
                    + "           ([dmid]"
                    + "           ,[dmoney]"
                    + "           ,[ddate]"
                    + "           ,[dremark]"
                    + "           ,[dmode]"
                    + "           ,[dparentid]"
                    + "           ,[czdh]"
                    + ",[zsje]"
                    + ",[jsr]"
                    + ",[skzh]"
                    + ",[skje],[班组])"
                    + "     VALUES"
                    + "           ('{0}'"
                    + "           ,'{1}'"
                    + "           ,'{2}'"
                    + "           ,'{3}'"
                    + "           ,'{4}'"
                    + "           ,{5}"
                    + "           ,'{6}'"
                    + ",{7}"
                    + ",'{8}'"
                    + ",'{9}'"
                    + ",{10},'{11}')", txtcardID.Text, txtsjcz.Text, datatime, txtczbz.Text.ToString(), "0", 0, txtdjh.Text, txtzsje.Text, chuanzhi.userID, comboBox1.Text, txtssje.Text, chuanzhi.banzu);
                conn1.Open();
                SqlCommand comm1 = new SqlCommand(sql1, conn1);
                comm1.ExecuteNonQuery();
                //插入收入明细表

                SqlConnection conn2 = new SqlConnection(constr);
                conn2.Open();
                string sql2 = string.Format("INSERT INTO [BHairDB].[dbo].[收入明细]([djh],[skje],[zsje],[djje],[yhje],[skfs],[djbz],[jsr],[djrq],[班组])VALUES('{0}',{1},'{2}','{3}',{4},'{5}','{6}','{7}','{8}','{9}')", txtdjh.Text, txtssje.Text, txtzsje.Text, txtsjcz.Text, 0, comboBox1.Text, txtczbz.Text, chuanzhi.userID, DateTime.Now.ToString("yyyy-MM-dd"), chuanzhi.banzu);
                SqlCommand comm2 = new SqlCommand(sql2, conn2);
                comm2.ExecuteNonQuery();
                conn.Open();
                decimal cz = sjcz + ye;
                //修改会员余额
                string sql = string.Format("UPDATE [BHairDB].[dbo].[member]SET [mbalance] = '{0}'WHERE mid='{1}'", cz, txtcardID.Text);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                MessageBox.Show("充值成功，本次充值" + sjcz + "元");
                chuanzhi cz2 = new chuanzhi();
                cz2.rizhi("会员充值" + ":"+txtcardID.Text+"-"+sjcz+"元");
                if (checkBox1.Checked)
                {
                    print();
                }
                djhshuaxin();
                djh();
                jiazai();

                conn.Close();
                conn1.Close();
                conn2.Close();
                
            }
            catch (Exception ex)
            {
                conn.Close();
                conn1.Close();

                MessageBox.Show("充值失败原因为" + ex.Message);
            }
        }
        //根据单号查询打印数据
        public DataSet cxdy()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT distinct [did]"
                + "      ,[dmid]"
                + "      ,[dmoney]"
                + "      ,[ddate]"
                + "      ,[dremark]"
                + "      ,[dmode]"
                + "      ,[dparentid]"
                + "      ,[czdh]"
                + "      ,[zsje]"
                + "      ,[jsr]"
                + "      ,[skzh]"
                + ",[skje]"
                + "      ,member.mname"
                + "      ,member.mphone"
                + "  FROM [deposit] join member on deposit.dmid=member.mid"
                + " where czdh='{0}'", txtdjh.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "deposit");
            conn.Close();
            return ds;
        }
        //打印
        private void button3_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员充值.repx");
            xr.DataSource = cxdy();
            xr.ShowPreview();
        }
        public void print()
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员充值.repx");
            xr.DataSource = cxdy();
            xr.ShowPreview();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员充值.repx");
            xr.Report.Name = "会员充值";
            xr.ShowDesignerDialog();
            xr.SaveLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员充值.repx");
            xr.Dispose();
        }
        public void shifoudayin()
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                string sql = "SELECT [czdy] FROM [BHairDB].[dbo].[sysconfig]";
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataReader sdr = comm.ExecuteReader();
                int sf = 0;
                while (sdr.Read())
                {
                    sf = Int32.Parse(sdr["czdy"].ToString());
                }
                if (sf == 1)
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }

            }
            catch (Exception eex)
            {
                MessageBox.Show(eex.Message);
            }

        }
        //自动打印
        private void checkBox1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                conn.Open();
                int yx = 0;

                if (checkBox1.Checked)
                {
                    yx = 1;
                }
                string sql = string.Format("UPDATE [BHairDB].[dbo].[sysconfig] SET [czdy] ={0}", yx);
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ee)
            {
                conn.Close();
                MessageBox.Show(ee.Message);
            }
        }

        private void txtcardID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                chuanzhi ca = new chuanzhi();
                if (ca.CheckPass(txtcardID.Text) == true)
                {
                    if (ca.Checkyxq(txtcardID.Text) == true)
                    {
                        try
                        {
                            SqlConnection conn = new SqlConnection(constr);
                            conn.Open();
                            string sql = string.Format("SELECT [mname],[mbalance] FROM [BHairDB].[dbo].[member] where mid='{0}'", txtcardID.Text);
                            SqlCommand comm = new SqlCommand(sql, conn);
                            SqlDataReader sdr = comm.ExecuteReader();
                            while (sdr.Read())
                            {
                                ckr = sdr["mname"].ToString();
                                ye = decimal.Parse(sdr["mbalance"].ToString());
                            }
                            txtckr.Text = ckr;
                            txtye.Text = ye.ToString();
                            conn.Close();
                        }
                        catch (Exception ex)
                        {

                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("此卡已过期！");
                        txtcardID.Text = null;
                    }

                }
                else
                {
                    MessageBox.Show("卡号不存在！");
                    txtcardID.Text = null;
                }

            }
            //样式设计

        }
    }
}
